/*
    获取用户信息

    维护用户的收件地址
        增
        删
        改
        查
*/
import * as UserService from '../service/user'
import * as OrderService from '../service/order'
import * as Content from '../constant'

function onBridgeReady(data){
    window.WeixinJSBridge.invoke(
       'getBrandWCPayRequest', {
           ...data
       },
       function(res){
       if(res.err_msg == "get_brand_wcpay_request:ok" ){
       // 使用以上方式判断前端返回,微信团队郑重提示：
             //res.err_msg将在用户支付成功后返回ok，但并不保证它绝对可靠。
             alert('支付成功')
       } 
    }); 
 }

export default {
    namespace: 'user',
    state: {
        user: {
            nickname: '',
            headimgurl: ''
        },
        addressList: [],
        addressInfo: {}
    },
    reducers: {
        // 
        user(state, { payload }) {
            return {
                ...state,
                user: payload
            }
        }
    },
    effects: {
        *login(action, { call, put }) {
            let res = yield call(UserService.login, action.payload)
            console.log('login :', res)
            localStorage[Content.TOKEN_KEY] = res['token']
            yield put({ type: 'user', payload: res.data })
        },
        *pay(action, { call }) {
            let res = yield call(OrderService.pay, action.payload)
            console.log(res)
            onBridgeReady(res.data)
        }
    }
}
